/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T27A_heter_bychild_outcome.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
Table A27: Heterogeneity of impact: baseline child outcomes
*******************************************************************************/

forvalues num=2/3 {
	gen group`num'_lindex=intlindex`num'
	gen group`num'_hindex=inthindex`num'
	gen group`num'_lsocindex=intlsocindex`num'
	gen group`num'_hsocindex=inthsocindex`num'
	gen group`num'_lefindex=intlefindex`num'
	gen group`num'_hefindex=inthefindex`num'
	gen group`num'_ltevir=intltevir`num'
	gen group`num'_htevir=inthtevir`num'

	
	}

label var ltevir

label var group2_ltevir "Below median NEP-B"
label var group2_htevir "Above median NEP-B"
label var group2_lefindex "Below median NEP-B"
label var group2_hefindex "Above median NEP-B"
label var group2_lsocindex "Below median NEP-B"
label var group2_hsocindex "Above median NEP-B"


label var group3_ltevir "Below median NEP-I"
label var group3_htevir "Above median NEP-I"
label var group3_lefindex "Below median NEP-I"
label var group3_hefindex "Above median NEP-I"
label var group3_lsocindex "Below median NEP-I"
label var group3_hsocindex "Above median NEP-I"

local finaloutcomes " zptevir_irtscore2 zcog_index zsoc_index" 
local list1 "finaloutcomes" 
#delimit ;
foreach f of local list1 {;

		foreach var of local `f' {;
					
			* PANEL 4: soc index at baseline;
				xi: xtreg `var' group2_lsocindex group2_hsocindex group3_lsocindex group3_hsocindex lsocindex, fe i(CENTRO_SALUD) ;
				estimates store `var'_SI;
				test  group2_lsocindex - group2_hsocindex==0;
				estadd scalar p2=r(p);
				test group3_lsocindex- group3_hsocindex==0;
				estadd scalar p3=r(p);

				test group2_lsocindex=group3_lsocindex;
				estadd scalar lsocindexp=r(p);
				test group2_hsocindex =group3_hsocindex;
				estadd scalar hsocindexp=r(p);
			
				qui sum `var' if e(sample) & TIPO==1;
				estadd scalar mean_y=r(mean);			
				
			* PANEL 4: ef index at baseline;
				xi: xtreg `var' group2_lefindex group2_hefindex group3_lefindex group3_hefindex lefindex, fe i(CENTRO_SALUD) ;
				estimates store `var'_CI;
				test  group2_lefindex - group2_hefindex==0;
				estadd scalar p2=r(p);
				test group3_lefindex- group3_hefindex==0;
				estadd scalar p3=r(p);

				test group2_lefindex=group3_lefindex;
				estadd scalar lefindexp=r(p);
				test group2_hefindex =group3_hefindex;
				estadd scalar hefindexp=r(p);
			
				qui sum `var' if e(sample) & TIPO==1;
				estadd scalar mean_y=r(mean);	
				
			* PANEL 4: vocubulary at baseline;
				xi: xtreg `var' group2_ltevir group2_htevir group3_ltevir group3_htevir ltevir, fe i(CENTRO_SALUD) ;
				estimates store `var'_LI;
				test  group2_ltevir - group2_htevir==0;
				estadd scalar p2=r(p);
				test group3_ltevir- group3_htevir==0;
				estadd scalar p3=r(p);

				test group2_ltevir=group3_ltevir;
				estadd scalar ltevirp=r(p);
				test group2_htevir =group3_htevir;
				estadd scalar htevirp=r(p);
			
				qui sum `var' if e(sample) & TIPO==1;
				estadd scalar mean_y=r(mean);	
					
				
				
				};
		
};

#delimit cr

  esttab zptevir_irtscore2_LI zcog_index_CI zsoc_index_SI using "${out_tables}/appendix/T27A_heter_bychild_outcome.csv", replace f ///	   
		cells("b(fmt(3)star)" "se(fmt(3)par)") ///
	    star(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 p2 p3 N, fmt(2 3 3 0) ///
		labels("R-squared" "P-value for lindex=hindex: NEP-B" "P-value for lindex=hindex: NEP-I" "Observations")) legend ///
		varlabels(group2_lcon "NEP-B Below median" group2_hcon "NEP-B Above median" group3_lcon "NEP-I  Below median" group3_hcon "NEP-I Above median") ///
		mtitles("Vocabulary Index" "Exec. Function Index" "Socioemotional Index" )   label								 
		
		
		
